<?php

/**
 * 管理后台
 * 
 * @author ShuangYa
 * @package Blog
 * @category Controller
 * @link http://www.sylingd.com/
 * @copyright Copyright (c) 2015 ShuangYa
 * @license http://lab.sylingd.com/go.php?name=blog&type=license
 */

namespace blog\controller;

use \sy\base\Controller;
use \sy\base\i18n;
use \sy\lib\YHtml;
use \sy\lib\db\YMysql;
use \sy\lib\YSecurity;
use \sy\lib\YCookie;
use \sy\tool\YFtp;
use \blog\libs\option;
use \blog\libs\BArticle;
use \blog\libs\BMeta;
use \blog\libs\BLink;

class Admin extends Controller {
	protected $password;
	public function __construct() {
		//强制不缓存
		header('expires: ' . date('d,d m y h:i:s', mktime(0, 0, 0, 1, 1, 2000)) . ' gmt');
		header('last-modified:' . gmdate('d,d m y h:i:s') . ' gmt');
		header('cache-control: private, no-cache,must-revalidate');
		header('pragma: no-cache');
		$this->password = option::get('password');
	}
	/**
	 * 检查登录状态
	 * @access protected
	 */
	protected function checkLogin($error = '') {
		if (isset($_REQUEST['auth'])) {
			$auth = $_REQUEST['auth'];
		} else {
			$auth = YCookie::get('auth');
		}
		if ($auth !== md5($this->password)) {
			switch ($error) {
				case 'boolean':
					return TRUE;
				case 403:
					Sy::httpStatus('403', TRUE);
				default:
					Sy::setMimeType('html');
					echo '<script type="text/javascript">window.location.href="' . Sy::createUrl('admin/login') . '";</script><a href="' . Sy::createUrl('admin/login') . '">Click here!</a>';
					exit;
			}
		} else {
			if (!isset($_REQUEST['auth'])) {
				YCookie::set(['name' => 'auth', 'value' => $auth, 'httponly' => TRUE]);
			}
			return TRUE;
		}
	}
}
